﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Library.Web.Data
{
    public class Repository
    {
        public LibDemoEntities Context { get; private set; }

        public Repository()
        {
            Context = new LibDemoEntities();
        }

        public void Save()
        {
            if (Context != null)
                Context.SaveChanges();
        }

        #region Books

        public IQueryable<Book> Books
        {
            get
            {
                return Context.Books
                    .Include("BookCategories.Category").OrderBy(k => k.Id);
            }
        }

        public void Add(Book book)
        {
            Context.Books.AddObject(book);
        }

        public Book GetBook(int bookId)
        {
            return Context.Books.Include("BookComments")
                .Where(b => b.Id == bookId)
                .FirstOrDefault();
        }

        #endregion

        #region Categories

        public IQueryable<Category> Categories
        {
            get { return Context.Categories; }
        }

        #endregion

        #region Comments

        public IQueryable<BookComment> BookComments(int bookId)
        {
            return Context.BookComments
                .Where(c => c.BookId == bookId)
                .OrderBy(c => c.Id);
        }

        #endregion
    }
}